﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DataAccessLayer;

namespace BusinessLogicLayer
{
    public class CT_GiaoViecBLL
    {
        QuanLyChamCongDataContext DB = new QuanLyChamCongDataContext();

        public List<CT_GIAOVIEC> LayDSCTGiaoViec(int maGV)
        {
            List<CT_GIAOVIEC> _ls = (from a in DB.CT_GIAOVIECs
                                     where a.MaGiaoViec == maGV
                                     select a).ToList();
            return _ls;
        }

        public List<DGVCTGIAOVIEC> LayDSCTGiaoViec2(int maGV)
        {
            List<DGVCTGIAOVIEC> _ls = (from a in DB.CT_GIAOVIECs
                                       where a.MaGiaoViec == maGV
                                       select new DGVCTGIAOVIEC()
                                       {
                                           MaGiaoViec = a.MaGiaoViec,
                                           MaCongViec = a.MaCongViec,
                                           TenCongViec = a.TenCongViec,
                                           DonVi = (a.DONVI.TenDonVi).ToString(),
                                           ChiTieu = (float)a.ChiTieu,
                                           KetQua = a.KetQua.Value.ToString(),
                                           HeSo = (float)a.HeSo
                                       }).ToList();
            return _ls;
        }

        public CT_GIAOVIEC LayCTGiaoViec(int macv)
        {
            return DB.CT_GIAOVIECs.SingleOrDefault(a => a.MaCongViec == macv);
        }

        public bool KiemTraTonTai(string _tenCongviec, int _madv, int _magv)
        {
            if (DB.CT_GIAOVIECs.Any(a => a.TenCongViec == _tenCongviec && a.MaDonVi == _madv && a.MaGiaoViec == _magv))
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public bool KiemTraTonTaiCongViec(int _magv)
        {
            if (DB.CT_GIAOVIECs.Any(a => a.MaGiaoViec == _magv))
                return true;
            else
                return false;
        }

        public void Them(CT_GIAOVIEC a)
        {
            DB.CT_GIAOVIECs.InsertOnSubmit(a);
            DB.SubmitChanges();
        }

        public void Xoa(int macv)
        {
            CT_GIAOVIEC cv = DB.CT_GIAOVIECs.SingleOrDefault(a => a.MaCongViec == macv);
            DB.CT_GIAOVIECs.DeleteOnSubmit(cv);
            DB.SubmitChanges();
        }

        public void Sua(CT_GIAOVIEC n)
        {
            CT_GIAOVIEC o = DB.CT_GIAOVIECs.SingleOrDefault(a => a.MaCongViec == n.MaCongViec);
            o.KetQua = n.KetQua;
            DB.SubmitChanges();
        }

        public bool KiemTraKetQua(int magv)
        {
            if (DB.CT_GIAOVIECs.Any(a => a.MaGiaoViec == magv && a.KetQua == null))
                return true;
            else
                return false;
        }
    }
}
